#include "gamedata.h"
#include <QDebug>
#include "GameData/jsontranslator.h"
#include "ResourceManger/filemanager.h"
GameData &GameData::Instance()
{
    static GameData instance;
    return instance;
}

GameData::~GameData()
{
    delete listModel;
}

void GameData::DeleteData(QString &name)
{
    if(stringData.contains(name))
        stringData.remove(name);
    else if(intData.contains(name))
        intData.remove(name);
    else if(boolData.contains(name))
        boolData.remove(name);
    else if(floatData.contains(name))
        floatData.remove(name);
    else
        return;
    for(int i = 0;i < list.count();i++)
    {
        if(list.at(i) == name)
        {
            list.removeAt(i);
            break;
        }
    }
    listModel->setStringList(list);
    visible.remove(name);
}

bool GameData::AddData(QString &name, int data)
{
    if(stringData.contains(name) || intData.contains(name) ||
            boolData.contains(name) || floatData.contains(name))
        return false;
    intData.insert(name,data);
    list<<name;
    listModel->setStringList(list);
    visible.insert(name,true);
    return true;
}
//调用此函数时禁止AddData（name,"xxxxx"）
//因为""会被识别为bool
//应先将data的值先用一个临时变量存起来，再用临时变量传参
//例：QString s = "";QString name = "名字";AddData(name,s);
bool GameData::AddData(QString &name, QString &data)
{
    if(stringData.contains(name) || intData.contains(name) ||
            boolData.contains(name) || floatData.contains(name))
        return false;
    stringData.insert(name,data);
    list<<name;
    listModel->setStringList(list);
    visible.insert(name,true);
    return true;
}

bool GameData::AddData(QString &name, float data)
{
    if(stringData.contains(name) || intData.contains(name) ||
            boolData.contains(name) || floatData.contains(name))
        return false;
    floatData.insert(name,data);
    list<<name;
    listModel->setStringList(list);
    visible.insert(name,true);
    return true;
}

bool GameData::AddData(QString &name, bool data)
{
    if(stringData.contains(name) || intData.contains(name) ||
            boolData.contains(name) || floatData.contains(name))
        return false;
    boolData.insert(name,data);
    list<<name;
    listModel->setStringList(list);
    visible.insert(name,true);
    return true;
}

bool GameData::ChangeName(QString &oldName, QString &newName)
{
    bool ans = false;
    if(stringData.contains(oldName)){
        ans = AddData(newName,stringData[oldName]);
    }
    else if(intData.contains(oldName)){
        ans = AddData(newName,intData[oldName]);
    }
    else if(floatData.contains(oldName)){
        ans = AddData(newName,floatData[oldName]);
    }
    else if(boolData.contains(oldName)){
        ans = AddData(newName,boolData[oldName]);
    }
    if(ans)
    {
        DeleteData(oldName);
    }
    return ans;
}

QStringList GameData::GetDataList()
{
    return list;
}

QStringListModel *GameData::GetDataListModel()
{
    return listModel;
}

DATATYPE GameData::GetDataType(const QString &name)
{
    if(stringData.contains(name))
        return STRING_;
    else if(intData.contains(name))
        return INT_;
    else if(boolData.contains(name))
        return BOOL_;
    else if(floatData.contains(name))
        return FLOAT_;
    else
        return NONE_;
}

GameData::GameData()
{
    listModel = new QStringListModel();
}

void GameData::RefreshData(RecordItem *item)
{
    JsonTranslator jt;
    if(item && item->data)
    {
        jt.ReadData(item->data);
    }
    else if(!item)
    {
        QString path = FileManager::Instance().gameDataPath;
        QJsonObject* data = FileManager::Instance().ReadJsonFile(path);
        jt.ReadData(data);
    }
    emit EventCenter::Instance().RefreshData();
}
